import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import java.io.InputStream;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import javax.microedition.media.PlayerListener;
import javax.microedition.media.control.VolumeControl;
public class Options extends Canvas implements PlayerListener{

	private MainMenu mainmenu;
	private Display display;
	private byte optionselected;
	private Player player;
	private VolumeControl vc;
	private InputStream is;
	private Reset reset;
	public Options(MainMenu mainmenu, Display display){
		this.mainmenu = mainmenu;
		this.display = display;
		optionselected = 25;
		reset = new Reset(mainmenu, this, display);
		is = getClass().getResourceAsStream("/caralarm.mp3");
		try{			
			player = Manager.createPlayer(is,"audio/mpeg");	
			player.addPlayerListener(this);
			player.realize();								
			player.prefetch();	
			vc = (VolumeControl) player.getControl("VolumeControl");
			if(vc != null){
				vc.setLevel(100);
			}			
		
		}
		catch(Exception e){}	
		setFullScreenMode(true);

	}
	public void playerUpdate(Player player, String event, Object evenData){
		if(event == (PlayerListener.END_OF_MEDIA)){				
			//try{
				//player.stop();
			//}
			//catch(Exception e){}
		}		
	}
	public void paint(Graphics g){
		g.setColor(255, 255, 255);
		g.fillRect(0, 0, 128, 160);
		g.setColor(0,255,255);
		//dong duoi dung de ve dong mau sang,
		g.fillRect(0, optionselected -2, 128, 15);					
		//con 2 dong tiep theo chi ve nhung mui ten don gian, chon cach nao cung duoc
		//g.drawString(">>>", 19, 25, 0);
		//g.drawString("<<<", 90, 25, 0);
		g.setColor(0, 0, 0);
		//g.drawLine(64,0,64,160);
		if(Option.language == true){
			if(Option.sound == true){
				g.drawString("Am Thanh: Bat", 27, 25, 0);				
			}
			else 
				g.drawString("Am Thanh: Tat", 27, 25, 0);
			if(Option.vibration == true)
				g.drawString("Rung: Bat", 40, 40, 0);	
			else
				g.drawString("Rung: Tat", 40, 40, 0);
			g.drawString("Cai Mat Ma", 37,55,0);
			g.drawString("Choi Lai Tu Dau", 25,70,0);	
			g.drawString("Tro ve", 95, 145, 0);
		}
		else{
			if(Option.sound == true){
				g.drawString("Sound: On", 40, 25, 0);				
			}
			else 
				g.drawString("Sound: Off", 40, 25, 0);
			if(Option.vibration == true)
				g.drawString("Vibrable: On", 35, 40, 0);
			else
				g.drawString("Vibrable: Off", 35, 40, 0);
			g.drawString("Set Password", 31,55,0);
			g.drawString("Reset Game", 37,70,0);	
			g.drawString("Back", 103, 145, 0);
		}
	}
	protected void keyPressed(int code){
		if(code == -7)
			display.setCurrent(mainmenu);
		if((code == 50)||(code == -1)){
			optionselected -=15;
			if(optionselected <25)
				optionselected  = 70;
		}
		if((code == 56)||(code == -2)){
			optionselected +=15;
			if(optionselected >70)
				optionselected  = 25;
		}
		if((optionselected == 25)&&((code == -5)||(code == 53))){				
			if(Option.sound == true){
				Option.sound = false;
				try{
					player.stop();
				}
				catch(Exception e){}	
			}
			else{
				Option.sound = true;	
				try{
					player.start();
				}
				catch(Exception e){}							
			}
		}
		else if((optionselected == 40)&&((code == -5)||(code == 53))){
			if(Option.vibration == true)
				Option.vibration = false;
			else{
				Option.vibration = true;	
				display.vibrate(500);					
			}
		}
		else if((optionselected == 40)&&((code == -5)||(code == 53))){
			if(Option.vibration == true)
				Option.vibration = false;
			else{
				Option.vibration = true;	
				display.vibrate(500);					
			}
		}
		else if((optionselected == 70)&&((code == -5)||(code == 53))){
			display.setCurrent(reset);
		}
		repaint();
	}
}